package com.mxpio.erp.purchase.controller;


import com.mxpio.erp.common.purchase.entity.AcceptanceOrder;
import com.mxpio.erp.common.purchase.service.AcceptanceOrderService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.CriteriaUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Api(value = "AcceptanceOrderController", tags = { "验收单数据接口" })
@RestController
@RequestMapping("/erp/purc/ao/")
public class AcceptanceOrderController {
    
    @Autowired
    private AcceptanceOrderService acceptanceOrderService;

    @GetMapping("list")
    @ApiOperation(value = "验收单列表", notes = "获取验收单（无分页）", httpMethod = "GET")
    public Result<List<AcceptanceOrder>> list(String criteria) throws UnsupportedEncodingException {
        Criteria c = CriteriaUtils.json2Criteria(criteria);
        List<AcceptanceOrder> acceptanceOrders = acceptanceOrderService.list(AcceptanceOrder.class, c);
        return Result.OK(acceptanceOrders);
    }

    @GetMapping("page")
    @ApiOperation(value = "验收单列表", notes = "获取验收单（分页）", httpMethod = "GET")
    public Result<Page<AcceptanceOrder>> page(String criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Criteria c = CriteriaUtils.json2Criteria(criteria);
        Page<AcceptanceOrder> acceptanceOrders = acceptanceOrderService.listPage(AcceptanceOrder.class, page, c);
        return Result.OK(acceptanceOrders);
    }

    @GetMapping("list/{bizNo}")
    @ApiOperation(value = "根据bizNo获取验收单", notes = "根据bizNo获取验收单", httpMethod = "GET")
    public Result<AcceptanceOrder> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
        AcceptanceOrder acceptanceOrder = acceptanceOrderService.getById(AcceptanceOrder.class, bizNo);
        return Result.OK(acceptanceOrder);
    }

    @PostMapping("add")
    @ApiOperation(value = "新增验收单", notes = "新增验收单", httpMethod = "POST")
    public Result<AcceptanceOrder> add(@RequestBody AcceptanceOrder acceptanceOrder) {
        acceptanceOrderService.save(acceptanceOrder);
        return Result.OK(acceptanceOrder);
    }

    @PutMapping("edit")
    @ApiOperation(value = "编辑验收单", notes = "编辑验收单", httpMethod = "PUT")
    public Result<AcceptanceOrder> edit(@RequestBody AcceptanceOrder acceptanceOrder) {
        acceptanceOrderService.update(acceptanceOrder);
        return Result.OK(acceptanceOrder);
    }

    @DeleteMapping("remove/{bizNo}")
    @ApiOperation(value = "根据bizNo删除验收单", notes = "根据bizNo删除验收单,多个bizNo用英文逗号分隔", httpMethod = "DELETE")
    public Result<AcceptanceOrder> remove(@PathVariable(name = "bizNo", required = true) String bizNos) {
        String[] bizNo = bizNos.split(",");
        for(String key : bizNo){
            acceptanceOrderService.remove(AcceptanceOrder.class, key);
        }
        return Result.OK();
    }
}
